


#include "string.h"
#include "stdio.h"


#include "string_tools.h"
#include "uart_printf.h"




uint8_t uitoa (const uint16_t nb,const uint16_t base,char str[])
{
char s[8];	
int i,j,n;
	
  if(base<=0)
  {
	return 0;//要基数大于0
  }
		
		
  memset(str,0,sizeof(s));	
	
  n = nb;	
  i=0;
	
  do{
    s[i]=n%base+'0'; i++;  //从个位数开始，取一位数字，小端
  }while ((n/=base)>0);//删除该数字	
	
	i--;//去掉结束符
  for(j=0;i>=0;i--,j++)//生成的数字是逆序的，所以要逆序输出
     str[j]=s[i];
	
   str[j]='\0';//结束符
	return j; //返回字符串长度
	
} 





/**
*@brief	 	把十进制数转化为字符型
*@param		c:要转化十进制数据
*@return	返回一个字符型数据
*/
static char c2d(uint8_t c	)
{
	if (c >= '0' && c <= '9')
		return c - '0';
	if (c >= 'a' && c <= 'f')
		return 10 + c -'a';
	if (c >= 'A' && c <= 'F')
		return 10 + c -'A';

	return (char)c;
}



/**
*@brief	 	字符转转化为8位整型函数---大端
*@param		str:要转化字符串， base:
*@return	num:返回转化后的整型数
*/
uint16_t atoi16(char* str,uint16_t base	)
{
  unsigned int num = 0;
  while (*str !=0)
          num = num * base + c2d(*str++);
  return num;
}




/**
输入：ip 字符串
输出: ip uint8_t
*/
void inet_addr_(unsigned char* addr,unsigned char *ip)
{
	int i;
	char taddr[30];
	char * nexttok;
	char num;
	strcpy(taddr,(char *)addr);

	nexttok = taddr;
	for(i = 0; i < 4 ; i++)
	{
		nexttok = strtok(nexttok,".");
		if(nexttok[0] == '0' && nexttok[1] == 'x')
			  num = atoi16(nexttok+2,0x10);
		else
			  num = atoi16(nexttok,10);

		ip[i] = num;
		nexttok = NULL;
	}
}




/*******************************************************************************
* Function Name :   可用   sprintf(attr_val_str_buf,"%d",  pattr_obj->data.DO);
* Description   :  三位整数 右对齐
*******************************************************************************/
void NumToStr(unsigned char ucNum,unsigned char *pStr)
{
  unsigned char ucTemp;
	unsigned char ucHundred;
	pStr[2] = ucNum%10 + '0';//个位
	ucTemp = (ucNum/10)%10;   //十位
	ucHundred = (ucNum/100)%10;   //百位

	if(ucTemp>0){  //十位 1 - 9
		pStr[1] = ucTemp + '0';
	}else if((ucTemp == 0)&&(ucHundred==0)){
		pStr[1] = ' ';
	}else{
	  pStr[1] = '0';
	}
//	ucTemp = (ucNum/100)%10;   //百位
	if(ucHundred>0){
		pStr[0] = ucHundred + '0';
	}else{
		pStr[0] = ' ';
	}


  LOG_NAME();
  printf("ucNum=%d,pStr=%s\n",ucNum,pStr);


}




void HexToStrLen(unsigned char *pHex,unsigned char *pStr,	unsigned int len)
{
unsigned	char ddl,ddh;
unsigned	int i;

  for(i = 0;i < len;i++)
	{
	  ddh = 48 + pHex[i] / 16;
	  ddl = 48 + pHex[i] % 16;

	  if(ddh > 57)
	  {
        ddh = ddh + 7;
	  }

	  if(ddl > 57)
	  {
	      ddl = ddl + 7;
	  }

	  pStr[i*2] = ddh;
	  pStr[i*2+1] = ddl;
	}

	pStr[i*2] = '\0';

}


//ret=0,查找失败，ret=1查找成功
uint8_t FindStringFast(char* s,const char* t)
{
  uint8_t ret;
	char*     ps;
const char* pt;
	
	ps=s;	
	pt=t;
	ret=0;
	
 while(*s)
 {
		ps=s; //更新
		 while(*pt)
		 {
			 if( *pt != *ps )
			 {
				 pt=t;//重头开始			 
				 break;  
			 }else{			 
				 pt++; //比较下一个
				 ps++; //比较下一个
			 }
		 }
		
		 if('\0'== *pt)		 
		 { //完全匹配
			 ret =1; //find fit string 
			 break;
		 }
	 
	 s++;//往后查找--每次只移动一个字符
	 
 }		
 
 return ret;
	
}



//ret<0，没有找到相同的字符串；
//ret>=0，成功找到相同的字符串；ret为-返回匹配字符串的起始下标
int16_t FindStringRetIndex(char* s,const char* t)
{
  int16_t ret,index;
	char*     ps;
const char* pt;
	
	ps=s;	
	pt=t;
	ret=0;
	index=0;
	
 while(*s)
 {
		ps=s; //更新
		 while(*pt)
		 {
			 if( *pt != *ps )
			 {
				 pt=t;//重头开始			 
				 break;  
			 }else{			 
				 pt++; //比较下一个
				 ps++; //比较下一个
			 }
		 }
		
		 if('\0'== *pt)		 
		 { //完全匹配
			 ret =index; //find fit string ---返回匹配字符串的起始下标
			 break;
		 }
	 
	 s++;//往后查找--每次只移动一个字符
	 index++;//
 }

  if( '\0'== *s )
  {//查找到末尾
    ret =-1;//没有完全匹配
  }	
 
 return ret;
	
}







 







